﻿Public Class SharedItemMediaInfo
    Private mPosterSizeAtLoad As Drawing.Size
    Private mPosterLocationAtLoad As Drawing.Point
    Private mPosterSizeAtNewImage As Drawing.Size
    Private mPosterLocationAtNewImage As Drawing.Point

    Public Sub UpdateView(ByVal item As Entities.SharedItem, ByVal files As Generic.List(Of Entities.SharedItemFile), ByVal mediaInfo As Entities.MediaInfo)
        mPosterSizeAtLoad = picPoster.Size
        mPosterLocationAtLoad = picPoster.Location
        ClearDisplay()
        UpdateSharedItem(item)
        UpdateMediaInfo(mediaInfo)
    End Sub

    Private Sub UpdateSharedItem(ByVal item As Entities.SharedItem)
        If item IsNot Nothing Then
            lblTitle.Text = item.Name
        End If
    End Sub

    Private Sub ClearDisplay()
        lblTitle.Text = "Loading..."
        picPoster.Image = Nothing
        pnlLogos.Controls.Clear()
        mNextLogoX = 2
    End Sub

    Private Sub UpdateMediaInfo(ByVal mediaInfo As Entities.MediaInfo)
        If mediaInfo IsNot Nothing Then
            lblTitle.Text = mediaInfo.FeatureTitle
            UpdateOpenMediaInfo(mediaInfo.OpenMediaInfo)
        End If
    End Sub
    Private Sub UpdateOpenMediaInfo(ByVal omi As Entities.OpenMediaInfo.OpenMediaInfo)
        If omi Is Nothing Then Return
        If omi.Feature IsNot Nothing Then
            If omi.Feature.Poster IsNot Nothing Then
                Using clsIO As New System.IO.MemoryStream(omi.Feature.Poster.Data)
                    Dim clsImage As New Drawing.Bitmap(clsIO)
                    picPoster.Image = clsImage

                    Dim factX As Double = picPoster.Image.Width / mPosterSizeAtLoad.Width
                    Dim factY As Double = picPoster.Image.Height / mPosterSizeAtLoad.Height
                    Dim fact As Double = CDbl(IIf(factX > factY, factX, factY))

                    Dim iNewW As Integer = CInt(picPoster.Image.Width / fact)
                    Dim iNewH As Integer = CInt(picPoster.Image.Height / fact)

                    Dim iNewX As Integer = CInt(mPosterLocationAtLoad.X + (mPosterSizeAtLoad.Width - iNewW) / 2)
                    Dim iNewY As Integer = CInt(mPosterLocationAtLoad.Y + (mPosterSizeAtLoad.Height - iNewH) / 2)

                    mPosterLocationAtNewImage = New Drawing.Point(iNewX, iNewY)
                    mPosterSizeAtNewImage = New Drawing.Size(iNewW, iNewH)

                    picPoster.SetBounds(iNewX, iNewY, iNewW, iNewH)
                End Using
            End If
            lblTitle.Text = String.Format("{0} ({1})", omi.Feature.Title, omi.Feature.Year)
        End If
        If omi.Streams IsNot Nothing Then
            For Each clsSI As Entities.OpenMediaInfo.StreamInfo In omi.Streams
                For Each clsVS As Entities.OpenMediaInfo.VideoStreamInfo In clsSI.VideoStreams
                    Select Case clsVS.Width
                        Case Is >= 1880 '=1920-40
                            AddIcon("FullHD", My.Resources.Video_FullHD, "Full HD Quality Video")
                        Case Is >= 1240 '=1280-30
                            AddIcon("HD", My.Resources.Video_HD, "HD Quality Video")
                        Case Is >= 716 '736-20
                            AddIcon("DVD", My.Resources.Video_DVD, "DVD Quality Video")
                    End Select
                    If clsVS.Format = "AVC" Then
                        AddIcon("AVC", My.Resources.H264, "H.264/AVC Video Encoding")
                    End If
                Next

                For Each clsAI As Entities.OpenMediaInfo.AudioStreamInfo In clsSI.AudioStreams
                    'If clsAI.Channels = "3/2/1" Or clsAI.Channels = "5.1" Then AddIcon("5.1", My.Resources., "5.1 Channel Audio")
                    If clsAI.Format = "DTS" Then AddIcon("DTS", My.Resources.DTS, "DTS Audio")
                    If clsAI.Format = "AC3" Then AddIcon("AC3", My.Resources.DolbyDigital, "Dolby Digital Audio")
                    If clsAI.Format = "AAC" Then AddIcon("AAC", My.Resources.AAC, "Advanced Audio Codec Audio")
                Next
            Next
        End If

    End Sub

    Private mNextLogoX As Integer = 2
    Private mIconKeys As New Generic.Dictionary(Of String, Boolean)
    Private Sub AddIcon(ByVal key As String, ByVal pic As Drawing.Icon, ByVal toolTip As String)
        If mIconKeys.ContainsKey(key) Then Return
        mIconKeys.Add(key, True)
        Dim clsPicBox As New PictureBox
        clsPicBox.SizeMode = PictureBoxSizeMode.StretchImage
        clsPicBox.Image = pic.ToBitmap
        clsPicBox.Size = pic.Size
        clsPicBox.Location = New Drawing.Point(mNextLogoX, 2)
        pnlLogos.Controls.Add(clsPicBox)
        ToolTip1.SetToolTip(clsPicBox, toolTip)
        mNextLogoX = clsPicBox.Location.X + clsPicBox.Size.Width + 6
    End Sub
    Private Sub AddIcon(ByVal key As String, ByVal pic As Drawing.Bitmap, ByVal toolTip As String)
        If mIconKeys.ContainsKey(key) Then Return
        mIconKeys.Add(key, True)
        Dim clsPicBox As New PictureBox
        clsPicBox.Image = pic
        clsPicBox.Size = pic.Size
        clsPicBox.Location = New Drawing.Point(mNextLogoX, 2)
        pnlLogos.Controls.Add(clsPicBox)
        ToolTip1.SetToolTip(clsPicBox, toolTip)
        mNextLogoX = clsPicBox.Location.X + clsPicBox.Size.Width + 6
    End Sub

    Private mResizePercent As Integer
    Private mResizeStep As Integer

    Private Sub picPoster_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles picPoster.MouseEnter
        If picPoster.Image IsNot Nothing Then
            mResizeStep = 25
            tmrResizePoster.Enabled = True
        End If
    End Sub

    Private Sub picPoster_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles picPoster.MouseLeave
        If picPoster.Image IsNot Nothing Then
            mResizeStep = -40
            tmrResizePoster.Enabled = True
        End If
    End Sub

    Private Sub tmrResizePoster_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrResizePoster.Tick
        mResizePercent += mResizeStep
        If mResizeStep < 0 Then
            'shrinking
            If mResizePercent <= 0 Then
                tmrResizePoster.Enabled = False
                mResizePercent = 0
            End If
        ElseIf mResizeStep > 0 Then
            If mResizePercent >= 200 Then
                tmrResizePoster.Enabled = False
                mResizePercent = 200
            End If
        End If
        Dim iNewWidth As Integer = CInt((picPoster.Image.Width - mPosterSizeAtNewImage.Width) * mResizePercent / 100 + mPosterSizeAtNewImage.Width)
        Dim iNewHeight As Integer = CInt((picPoster.Image.Height - mPosterSizeAtNewImage.Height) * mResizePercent / 100 + mPosterSizeAtNewImage.Height)
        picPoster.Size = New Drawing.Size(iNewWidth, iNewHeight)
    End Sub
End Class
